package com.huangpuweb.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.huangpuweb.api.service.HiveZdk57ApiService;
import com.huangpuweb.api.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 消防相关接口
 */
@Controller
@RequestMapping(value = "/fireController")
public class FireController {

    @Autowired
    public HiveZdk57ApiService hiveZdk57ApiService;

    /**
     * 获取当月警情
     */
    @RequestMapping(value = "/getNowMonthCount")
    public void getNowMonthCount(HttpServletRequest request, HttpServletResponse response) throws Exception{
        JSONObject resultObj = new JSONObject();
        Integer count =  hiveZdk57ApiService.getNowMonthCount();

        resultObj.put("error_code", 0);
        resultObj.put("error_msg", "获取成功");
        resultObj.put("data", count);
        responseData(resultObj.toString(), response);

    }

    /**
     * 获取当月警情
     */
    @RequestMapping(value = "/getNowYearCount")
    public void getNowYearCount(HttpServletRequest request, HttpServletResponse response) throws Exception{
        JSONObject resultObj = new JSONObject();
        Integer count =  hiveZdk57ApiService.getNowYearCount();

        resultObj.put("error_code", 0);
        resultObj.put("error_msg", "获取成功");
        resultObj.put("data", count);
        responseData(resultObj.toString(), response);

    }


    /**
     * 获取消防站警情分析
     * @param
     * @return
     */
    @RequestMapping(value = "/getInfoByFireStation")
    public void getInfoByFireStation(@RequestParam String year, HttpServletResponse response) throws Exception{
        JSONObject resultObj = new JSONObject();
        //获取当前年和之前的五年
        List<Map<String, Object>> list = new ArrayList<>();
        list = hiveZdk57ApiService.getInfoByFireStation(year);
        resultObj.put("error_code", 0);
        resultObj.put("error_msg", "获取成功");
        resultObj.put("data", list);
        responseData(resultObj.toString(), response);
    }



    /**
     * 警情类型分析
     * @param
     * @return
     */
    @RequestMapping(value = "/getInfoByFireType")
    public void getInfoByFireType(HttpServletRequest request, HttpServletResponse response) throws Exception{
        JSONObject resultObj = new JSONObject();
        List<String> yearList = getNowSixYear();
        //获取当前年和之前的五年
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String,List<Map<String, Object>> > resultMap  = new HashMap<>();
        for(String str : yearList){
            list = hiveZdk57ApiService.getInfoByFireType(str);
            resultMap.put(str, list);
        }

        resultObj.put("error_code", 0);
        resultObj.put("error_msg", "获取成功");
        resultObj.put("data", resultMap);
        responseData(resultObj.toString(), response);
    }

    /***
     *
     * @return
     */
    private List<String> getNowSixYear(){
        List<String> yearList = new ArrayList<>();
        String yearStr = DateUtil.getNowYear();
        Integer yearInt = Integer.valueOf(yearStr);
        for (int i = 0; i < 6 ; i++) {
            yearList.add(String.valueOf(yearInt - i));
        }
        return yearList;
    }



    private void responseErrorData(String string, HttpServletResponse resp) throws Exception {
        // TODO Auto-generated method stub
        JSONObject resultObj = new JSONObject();
        resultObj.put("errorcode", 1);
        resultObj.put("message", string);
        responseData(resultObj.toString(), resp);
    }

    private void responseData(String string, HttpServletResponse resp) throws Exception {
        // TODO Auto-generated method stub
        responseDate("application/json;charset=UTF-8", string, resp);
    }

    private void responseDate(String contentType, String value, HttpServletResponse resp) throws Exception {
        resp.setContentType(contentType);
        resp.getWriter().write(value);
    }


}
